/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCommesse.java
 *
 * Created on 3-ago-2011, 15.34.08
 */
package UI;

import UI.clienti.dialogs.dlgClientiSearch;
import UI.commesse.dialogs.dlgCommesseSearch;
import UI.commesse.dialogs.dlgEditArticoli;
import classes.Articoli_Componenti;
import classes.Commessa;
import classes.Commesse_Articoli;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mrossetti
 */
public class frmCommesse extends javax.swing.JFrame {

    /** Creates new form frmCommesse */
    Commessa Com;
    ArrayList<Commesse_Articoli> Articoli;
    ArrayList<Articoli_Componenti> Componenti;
    public frmCommesse() {
        initComponents();
        BufferedImage img = null;
        try{
            img = ImageIO.read(this.getClass().getResourceAsStream("/resources/commesse/kfm.png"));
        }catch(Exception e){
            e.printStackTrace();
        }
        setIconImage(img);
        setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width)/2, (Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height)/2);
        Com = new Commessa();
        BoundCommessa();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        P = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtIDCommessa = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCodice = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCliente = new javax.swing.JTextField();
        cmdSearch = new javax.swing.JButton();
        cmdSearchCliente = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        SP = new javax.swing.JScrollPane();
        TabArticoli = new javax.swing.JTable();
        cmdAddArticoli = new javax.swing.JButton();
        cmdDeleteArticoli = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        SP2 = new javax.swing.JScrollPane();
        TabComponenti = new javax.swing.JTable();
        cmdNuovo = new javax.swing.JButton();
        cmdSalva = new javax.swing.JButton();
        cmdCancella = new javax.swing.JButton();
        cmdEsci = new javax.swing.JButton();
        SP3 = new javax.swing.JScrollPane();
        txtNote = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        cmdDeleteCliente = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gestione commesse");

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("ID Commessa");

        txtIDCommessa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtIDCommessaFocusLost(evt);
            }
        });

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Codice");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Nome");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Cliente");

        txtCliente.setEditable(false);

        cmdSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/search.png"))); // NOI18N
        cmdSearch.setToolTipText("Ricerca commessa");
        cmdSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSearchActionPerformed(evt);
            }
        });

        cmdSearchCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/search.png"))); // NOI18N
        cmdSearchCliente.setToolTipText("Ricerca cliente");
        cmdSearchCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSearchClienteActionPerformed(evt);
            }
        });

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Articoli");

        TabArticoli.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID Articolo", "Codice", "Nome", "Quantità"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TabArticoli.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        TabArticoli.getTableHeader().setReorderingAllowed(false);
        SP.setViewportView(TabArticoli);

        cmdAddArticoli.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/add.png"))); // NOI18N
        cmdAddArticoli.setToolTipText("Aggiungi telefono");
        cmdAddArticoli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAddArticoliActionPerformed(evt);
            }
        });

        cmdDeleteArticoli.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/remove.png"))); // NOI18N
        cmdDeleteArticoli.setToolTipText("Cancella articoli");
        cmdDeleteArticoli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdDeleteArticoliActionPerformed(evt);
            }
        });

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Componenti");

        TabComponenti.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID Componente", "Codice", "Nome", "Quantità"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TabComponenti.getTableHeader().setReorderingAllowed(false);
        SP2.setViewportView(TabComponenti);

        cmdNuovo.setText("Nuovo");
        cmdNuovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuovoActionPerformed(evt);
            }
        });

        cmdSalva.setText("Salva");
        cmdSalva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSalvaActionPerformed(evt);
            }
        });

        cmdCancella.setText("Cancella");
        cmdCancella.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancellaActionPerformed(evt);
            }
        });

        cmdEsci.setText("Esci");
        cmdEsci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEsciActionPerformed(evt);
            }
        });

        txtNote.setColumns(20);
        txtNote.setRows(5);
        SP3.setViewportView(txtNote);

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Note");

        cmdDeleteCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/remove.png"))); // NOI18N
        cmdDeleteCliente.setToolTipText("Cancella cliente");
        cmdDeleteCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdDeleteClienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PLayout = new javax.swing.GroupLayout(P);
        P.setLayout(PLayout);
        PLayout.setHorizontalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PLayout.createSequentialGroup()
                        .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(PLayout.createSequentialGroup()
                                .addComponent(txtIDCommessa, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtCodice, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                            .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PLayout.createSequentialGroup()
                                .addComponent(txtCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdSearchCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdDeleteCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PLayout.createSequentialGroup()
                                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(SP2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
                                    .addComponent(SP, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
                                    .addComponent(SP3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(cmdAddArticoli, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cmdDeleteArticoli, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(PLayout.createSequentialGroup()
                        .addComponent(cmdNuovo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdSalva)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdCancella)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 241, Short.MAX_VALUE)
                        .addComponent(cmdEsci)))
                .addContainerGap())
        );
        PLayout.setVerticalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtIDCommessa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cmdSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCodice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(cmdDeleteCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdSearchCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PLayout.createSequentialGroup()
                        .addComponent(cmdAddArticoli, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdDeleteArticoli, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(SP, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(SP2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SP3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdNuovo)
                    .addComponent(cmdSalva)
                    .addComponent(cmdCancella)
                    .addComponent(cmdEsci))
                .addContainerGap())
        );

        getContentPane().add(P, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cmdSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSearchActionPerformed
        dlgCommesseSearch Search = new dlgCommesseSearch(this, true);
        Search.setVisible(true);
        Com.load();
        BoundCommessa();
}//GEN-LAST:event_cmdSearchActionPerformed

private void cmdSearchClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSearchClienteActionPerformed
        dlgClientiSearch Search = new dlgClientiSearch(this, true);
        Search.setVisible(true);
        Com.getCliente().load();
        txtCliente.setText(Com.getCliente().getRagioneSociale());
}//GEN-LAST:event_cmdSearchClienteActionPerformed

private void cmdAddArticoliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAddArticoliActionPerformed
        ((DefaultTableModel)TabArticoli.getModel()).addRow(new Object[TabArticoli.getColumnCount()]);
        TabArticoli.getSelectionModel().setSelectionInterval(TabArticoli.getRowCount()-1, TabArticoli.getRowCount()-1);
        TabArticoli.setValueAt("", TabArticoli.getSelectedRow(), 0);
        TabArticoli.setValueAt("", TabArticoli.getSelectedRow(), 1);
        TabArticoli.setValueAt("", TabArticoli.getSelectedRow(), 2);
        dlgEditArticoli Dlg = new dlgEditArticoli(this, TabArticoli, true);
        Dlg.setVisible(true);
}//GEN-LAST:event_cmdAddArticoliActionPerformed

private void cmdDeleteArticoliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeleteArticoliActionPerformed
        if(TabArticoli.getSelectedRow() >= 0){
            if(JOptionPane.showConfirmDialog(this, "Cancellare il numero selezionato?", "The WareHouse", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                ((DefaultTableModel)TabArticoli.getModel()).removeRow(TabArticoli.getSelectedRow());
            }
        }
}//GEN-LAST:event_cmdDeleteArticoliActionPerformed

private void cmdNuovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuovoActionPerformed
        Com = new Commessa();
        BoundCommessa();
}//GEN-LAST:event_cmdNuovoActionPerformed

private void cmdSalvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSalvaActionPerformed
        try{
            if(ControllaForm()){
                if(!txtIDCommessa.getText().equals(""))Com.setIDCommessa(Integer.parseInt(txtIDCommessa.getText()));
                Com.setCodiceCommessa(txtCodice.getText());
                Com.setNomeCommessa(txtNome.getText());
                Com.setNote(txtNote.getText());
                if(Com.save()){
                    JOptionPane.showMessageDialog(this, "Salvataggio Effettuato", "The WareHouse", JOptionPane.INFORMATION_MESSAGE);
                    Com.load(txtCodice.getText());
                    BoundCommessa();
                }else{
                    JOptionPane.showMessageDialog(this, "Salvataggio non Effettuato","The WareHouse", JOptionPane.ERROR_MESSAGE);
                }
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.getMessage(),"The WareHouse", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_cmdSalvaActionPerformed

private void cmdCancellaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancellaActionPerformed
        if(Com.getIDCommessa() >= 0){
            if(JOptionPane.showConfirmDialog(this, "Si desidera cancellare la commessa selezionata?","The WareHouse", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                if(Com.delete()){
                    JOptionPane.showMessageDialog(this, "Commessa cancellata", "The WareHouse", JOptionPane.INFORMATION_MESSAGE);
                    Com = new Commessa();
                    BoundCommessa();
                }else{
                    JOptionPane.showMessageDialog(this, "Cancellazione annullata a causa di un errore sul database", "The WareHouse", JOptionPane.WARNING_MESSAGE);
                }
            }
        }
}//GEN-LAST:event_cmdCancellaActionPerformed

private void cmdEsciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEsciActionPerformed
    setVisible(false);
    dispose();
}//GEN-LAST:event_cmdEsciActionPerformed

private void txtIDCommessaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtIDCommessaFocusLost
    if(!txtIDCommessa.getText().trim().equals("")){
        try{
            Com.setIDCommessa(Integer.parseInt(txtIDCommessa.getText().trim()));
            Com.load();
            BoundCommessa();
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
    }
}//GEN-LAST:event_txtIDCommessaFocusLost

private void cmdDeleteClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeleteClienteActionPerformed
    txtCliente.setText("");
}//GEN-LAST:event_cmdDeleteClienteActionPerformed

public void BoundCommessa(){
    if(Com.getIDCommessa() > 0){
        txtIDCommessa.setText(""+Com.getIDCommessa());
    }else{
        txtIDCommessa.setText("");
    }
    txtCliente.setText(Com.getCliente().getRagioneSociale());
    txtCodice.setText(Com.getCodiceCommessa());
    txtNome.setText(Com.getNomeCommessa());
    txtNote.setText(Com.getNote());
    BoundArticoli();
}

public void BoundArticoli(){
    Articoli = Com.getArticoli();
    ArrayList<Articoli_Componenti> temp = new ArrayList<Articoli_Componenti>();
    ((DefaultTableModel)TabArticoli.getModel()).setRowCount(0);
    for(int i = 0;i<Articoli.size();i++){
        ((DefaultTableModel)TabArticoli.getModel()).addRow(new Object[TabArticoli.getColumnCount()]);
        TabArticoli.setValueAt(Articoli.get(i).getIDArticolo(), i, 0);
        TabArticoli.setValueAt(Articoli.get(i).getCodiceArticolo(), i, 1);
        TabArticoli.setValueAt(Articoli.get(i).getNomeArticolo(), i, 2);
        TabArticoli.setValueAt(Articoli.get(i).getQta(), i, 3);
        if(temp.isEmpty()){
            Articoli.get(i).load();
            temp = Articoli.get(i).getComponenti();
            for(int h = 0;h<temp.size();h++){
                temp.get(h).setQta(temp.get(h).getQta() * Articoli.get(i).getQta());
            }
        }else{
            for(int k = 0;k<Articoli.get(i).getComponenti().size();k++){
                for(int j = 0;j<temp.size();j++){
                    if(temp.get(j).getCodice_componente().equals(Articoli.get(i).getComponenti().get(k).getCodice_componente())){
                        temp.get(j).load();
                        temp.get(j).setQta(temp.get(j).getQta() + (Articoli.get(i).getComponenti().get(k).getQta() * Articoli.get(i).getQta()));
                        break;
                    }else{
                        Articoli_Componenti temp2 = Articoli.get(i).getComponenti().get(k);
                        temp2.setQta(temp2.getQta() * Articoli.get(i).getQta());
                        temp.add(temp2);
                    }
                }
            }
        }
    }
    Componenti = temp;
    BoundComponenti();
}

public void BoundComponenti(){
    ((DefaultTableModel)TabComponenti.getModel()).setRowCount(0);
    for(int i = 0;i<Componenti.size();i++){
        ((DefaultTableModel)TabComponenti.getModel()).addRow(new Object[TabComponenti.getColumnCount()]);
        TabComponenti.setValueAt(Componenti.get(i).getIDComponente(), TabComponenti.getRowCount()-1, 0);
        TabComponenti.setValueAt(Componenti.get(i).getCodice_componente(), TabComponenti.getRowCount()-1, 1);
        TabComponenti.setValueAt(Componenti.get(i).getNomeComponente(), TabComponenti.getRowCount()-1, 2);
        TabComponenti.setValueAt(Componenti.get(i).getQta(), TabComponenti.getRowCount()-1, 3);
    }
}

private boolean ControllaForm(){
    if(txtCodice.getText().equals("")){
       JOptionPane.showMessageDialog(this, "Inserire un codice commessa", "The WareHouse", JOptionPane.WARNING_MESSAGE);
       return false;
    }
    if(txtCliente.getText().equals("")){
       if(JOptionPane.showConfirmDialog(this, "Nessun cliente selezionato, continuare comunque?", "The WareHouse", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE) == JOptionPane.NO_OPTION){
           return false;
       }
    }
    if(TabArticoli.getRowCount() == 0){
       JOptionPane.showMessageDialog(this, "Selezionare almeno un articolo", "The WareHouse", JOptionPane.WARNING_MESSAGE);
       return false;        
    }
    return true;
}

public void setCommessa(Commessa commessa){
    Com = commessa;
}

public Commessa getCommessa(){
    return Com;
}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel P;
    private javax.swing.JScrollPane SP;
    private javax.swing.JScrollPane SP2;
    private javax.swing.JScrollPane SP3;
    private javax.swing.JTable TabArticoli;
    private javax.swing.JTable TabComponenti;
    private javax.swing.JButton cmdAddArticoli;
    private javax.swing.JButton cmdCancella;
    private javax.swing.JButton cmdDeleteArticoli;
    private javax.swing.JButton cmdDeleteCliente;
    private javax.swing.JButton cmdEsci;
    private javax.swing.JButton cmdNuovo;
    private javax.swing.JButton cmdSalva;
    private javax.swing.JButton cmdSearch;
    private javax.swing.JButton cmdSearchCliente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField txtCliente;
    private javax.swing.JTextField txtCodice;
    private javax.swing.JTextField txtIDCommessa;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextArea txtNote;
    // End of variables declaration//GEN-END:variables
}
